package com.kucai.yifaqapp.ui.fragment.homePageFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.ResBanner;
import com.kucai.yifaqapp.ui.activity.AlllotteryActivity;
import com.kucai.yifaqapp.ui.activity.BaseActivity;
import com.kucai.yifaqapp.ui.activity.GameDetailActivityFinal;
import com.kucai.yifaqapp.ui.activity.WebActivity;
import com.kucai.yifaqapp.util.EventBus.GameDetailConstances;
import com.kucai.yifaqapp.util.OnItemClickListener;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/11/10.
 */

public class CaiPiaoFragment extends Fragment {
    private RecyclerView gameList;
    private ArrayList<ResBanner.DataBeanX.GamesBean> gamedata = new ArrayList<>();//左侧游戏
    private String serverurl;//在线客服
    Adapter adapter;//    @Nullable

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.caipiao_fragment, container, false);
        gameList = view.findViewById(R.id.game_list);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        gameList.setLayoutManager(gridLayoutManager);
        gameList.setHasFixedSize(true);
        adapter = new Adapter();
        gameList.setAdapter(adapter);
        return view;
    }

    public void setDatas(ArrayList<ResBanner.DataBeanX.GamesBean> mgamedata, String mserverurl) {
        this.gamedata = mgamedata;
        this.serverurl = mserverurl;
        adapter.notifyDataSetChanged();
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public Adapter.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Adapter.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.game_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final Adapter.MViewHolder holder, final int position) {

            if (gamedata.size() >= 7 && position == 7 && gamedata.size() < 10) {
                holder.geme_tv.setText("全部彩票");
                Glide.with(getContext()).load(R.mipmap.left_j).into(holder.game_iv);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(getContext(), AlllotteryActivity.class));
                    }
                });
            } else if (gamedata.size() >= 7 && position == 8 && gamedata.size() < 10) {
                holder.geme_tv.setText("在线客服");
                Glide.with(getContext()).load(R.mipmap.leftk).into(holder.game_iv);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (serverurl != null) {
                            Intent intent = new Intent(getContext(), WebActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("url", serverurl);
                            bundle.putString("title", "在线客服");
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    }
                });
            } else if (gamedata.size() >= 10 && gamedata.size() < 13 && position == 10) {
                holder.geme_tv.setText("全部彩票");
                Glide.with(getContext()).load(R.mipmap.left_j).into(holder.game_iv);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(getContext(), AlllotteryActivity.class));
                    }
                });
            } else if (gamedata.size() >= 10 && gamedata.size() < 13 && position == 11) {
                holder.geme_tv.setText("在线客服");
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (serverurl != null) {
                            Intent intent = new Intent(getContext(), WebActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("url", serverurl);
                            bundle.putString("title", "在线客服");
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    }
                });
            } else if (gamedata.size() < 7 && position == gamedata.size()) {
                holder.geme_tv.setText("全部彩票");
                Glide.with(getContext()).load(R.mipmap.left_j).into(holder.game_iv);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(getContext(), AlllotteryActivity.class));
                    }
                });
            } else if (gamedata.size() < 7 && position == gamedata.size() + 1) {
                if (position == gamedata.size() + 1) {
                    holder.geme_tv.setText("在线客服");
                    Glide.with(getContext()).load(R.mipmap.leftk).into(holder.game_iv);
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (serverurl != null) {
                                Intent intent = new Intent(getContext(), WebActivity.class);
                                Bundle bundle = new Bundle();
                                bundle.putString("url", serverurl);
                                bundle.putString("title", "在线客服");
                                intent.putExtras(bundle);
                                startActivity(intent);
                            }
                        }
                    });
                }
            } else if (gamedata.size() >= 13 && position == 13) {
                holder.geme_tv.setText("全部彩票");
                Glide.with(getContext()).load(R.mipmap.left_j).into(holder.game_iv);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(getContext(), AlllotteryActivity.class));
                    }
                });
            } else if (gamedata.size() >= 13 && position == 14) {
//                if (position == gamedata.size() + 1) {
                holder.geme_tv.setText("在线客服");
                Glide.with(getContext()).load(R.mipmap.leftk).into(holder.game_iv);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (serverurl != null) {
                            Intent intent = new Intent(getContext(), WebActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("url", serverurl);
                            bundle.putString("title", "在线客服");
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    }
                });
//                }
            } else {
                if (gamedata != null && gamedata.size() != 0) {
                    holder.geme_tv.setText(gamedata.get(position).game_name);
                    int sre = gamedata.get(position).game_id;
                    switch (sre) {

                        case 80:
                            Glide.with(getContext()).load(R.mipmap.mssc).into(holder.game_iv);
                            break;
                        case 50:
                            Glide.with(getContext()).load(R.mipmap.bjsc).into(holder.game_iv);
                            break;
                        case 82:
                            Glide.with(getContext()).load(R.mipmap.msft).into(holder.game_iv);
                            break;
                        case 99:
                            Glide.with(getContext()).load(R.mipmap.xgpm).into(holder.game_iv);
                            break;
                        case 70:
                            Glide.with(getContext()).load(R.mipmap.xglhc).into(holder.game_iv);
                            break;
                        case 81:
                            Glide.with(getContext()).load(R.mipmap.msssc).into(holder.game_iv);
                            break;
                        case 1:
                            Glide.with(getContext()).load(R.mipmap.cqssc).into(holder.game_iv);
                            break;
                        case 4:
                            Glide.with(getContext()).load(R.mipmap.xjssc).into(holder.game_iv);
                            break;
                        case 60:
                            Glide.with(getContext()).load(R.mipmap.gdklsf).into(holder.game_iv);
                            break;
                        case 91:
                            Glide.with(getContext()).load(R.mipmap.msnn).into(holder.game_iv);
                            break;
                        case 90:
                            Glide.with(getContext()).load(R.mipmap.pksnn).into(holder.game_iv);
                            break;
                        case 61:
                            Glide.with(getContext()).load(R.mipmap.cqxync).into(holder.game_iv);
                            break;
                        case 66:
                            Glide.with(getContext()).load(R.mipmap.pcdd).into(holder.game_iv);
                            break;
                        case 65:
                            Glide.with(getContext()).load(R.mipmap.bjklb).into(holder.game_iv);
                            break;
                        case 86:
                            Glide.with(getContext()).load(R.mipmap.msks).into(holder.game_iv);
                            break;
                        case 85:
                            Glide.with(getContext()).load(R.mipmap.xylhc).into(holder.game_iv);
                            break;
                        case 10:
                            Glide.with(getContext()).load(R.mipmap.jsks).into(holder.game_iv);
                            break;
                        case 12:
                            Glide.with(getContext()).load(R.mipmap.gxks).into(holder.game_iv);
                            break;
                        case 13:
                            Glide.with(getContext()).load(R.mipmap.hubks).into(holder.game_iv);
                            break;
                        case 11:
                            Glide.with(getContext()).load(R.mipmap.ahks).into(holder.game_iv);
                            break;
                        case 16:
                            Glide.with(getContext()).load(R.mipmap.gsks).into(holder.game_iv);
                            break;
                        case 15:
                            Glide.with(getContext()).load(R.mipmap.hbks).into(holder.game_iv);
                            break;
                        case 18:
                            Glide.with(getContext()).load(R.mipmap.gzks).into(holder.game_iv);
                            break;
                        case 21:
                            Glide.with(getContext()).load(R.mipmap.gdsyxw).into(holder.game_iv);
                            break;
                        case 112:
                            Glide.with(getContext()).load(R.mipmap.txffc).into(holder.game_iv);
                            break;
                        case 113:
                            Glide.with(getContext()).load(R.mipmap.qqffc).into(holder.game_iv);
                            break;
                        case 801:
                            Glide.with(getContext()).load(R.mipmap.kssc).into(holder.game_iv);
                            break;
                        //快速飞艇
                        case 802:
                            Glide.with(getContext()).load(R.mipmap.ksft).into(holder.game_iv);
                            break;
                        //快速时时彩
                        case 803:
                            Glide.with(getContext()).load(R.mipmap.ksssc).into(holder.game_iv);
                            break;
                        case 804:
                            Glide.with(getContext()).load(R.mipmap.twxyft).into(holder.game_iv);
                            break;
                        case 901:
                            Glide.with(getContext()).load(R.mipmap.sfsc).into(holder.game_iv);
                            break;
                        //快速飞艇
                        case 902:
                            Glide.with(getContext()).load(R.mipmap.sfssc).into(holder.game_iv);
                            break;
                        //快速时时彩
                        case 903:
                            Glide.with(getContext()).load(R.mipmap.jslhc).into(holder.game_iv);
                            break;
                        case 904:
                            Glide.with(getContext()).load(R.mipmap.sflhc).into(holder.game_iv);
                            break;
                        //幸运飞艇
                        case 55:
                            Glide.with(getContext()).load(R.mipmap.ic_launcher).into(holder.game_iv);
                            break;
                        default:
                            break;
                    }
                    holder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(getContext(), GameDetailActivityFinal.class);
                            GameDetailConstances.GameId = gamedata.get(position).game_id;
                            GameDetailConstances.GameName = gamedata.get(position).game_name + "";
                            intent.putExtra("gameid", gamedata.get(position).game_id + "");
                            intent.putExtra("gamename", gamedata.get(position).game_name + "");
                            if (Info.user != null) {
                                startActivity(intent);
                            } else {
                                ((BaseActivity) getActivity()).startLogin();
                            }
                        }
                    });
                }
            }
        }

        @Override
        public int getItemCount() {
            try {
                if (gamedata.size() >= 7 && gamedata.size() < 10) {
                    return 9;
                } else if (gamedata.size() >= 10 && gamedata.size() < 13) {
                    return 12;
                } else if (gamedata.size() >= 13) {
                    return 15;
                } else {
                    return gamedata.size() + 2;
                }
            } catch (Exception e) {
                return 0;
            }
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView geme_tv;
            ImageView game_iv;

            MViewHolder(View itemView) {
                super(itemView);
                geme_tv = (TextView) itemView.findViewById(R.id.geme_tv);
                game_iv = (ImageView) itemView.findViewById(R.id.game_iv);

            }
        }
    }
}
